#pragma once
#include <assert.h>
#include <iostream>

using namespace std;

namespace WX 
{
	template <class T>
	struct list_node
	{
		struct list_node* _next;
		struct list_node* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};


	template <class T, class ref, class ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, ref, ptr> self;

		Node* _node;

		list_iterator(Node* node)
			: _node(node)
		{}

		ref operator*()
		{
			return _node->_data;
		}

		ptr operator->()
		{
			return &_node->_data;
		}

		self operator++()
		{
			_node = _node->_next;
			return *this;
		}

		self operator++(int)
		{
			Node* tmp(_node);
			_node = _node->_next;
			return tmp;
		}

		self operator--()
		{
			_node = _node->_prev;
			return*this;
		}

		self operator--(int)
		{
			Node* tmp(_node);
			_node = _node->_prev;
			return tmp;
		}

		bool operator==(const self& it)
		{
			return _node == it->_node;
		}

		bool operator!=(const self& it)
		{
			return _node != it._node;
		}

	};

	template <class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;

		void Emptyinit()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			Emptyinit();
		}

		
		list(initializer_list<T> it)
		{
			Emptyinit();
			for (auto& e : it)
			{
				push_back(e);
			}
		}

		list(const list<T>& it)
		{
			Emptyinit();
			for (auto& e : it)
			{
				push_back(e);
			}
		}

		void swap(list<T>& it)
		{
			std::swap(_head, it._head);
			std::swap(_size, it._size);
		}

		list<T>& operator=(list<T> it)
		{
			swap(it);
			return *this;
		}

		void push_back(const T& x)
		{
			//Node* newnode = new Node(x);
			//Node* tail = _head->_prev;

			//newnode->_prev = tail;
			//tail->_next = newnode;
			//newnode->_next = _head;
			//_head->_prev = newnode;

			//_size++;

			insert(end(), x);

		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			earse(--end());
		}

		void pop_front()
		{
			earse(begin());
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		iterator earse(iterator pos)
		{
			assert(pos != end());

			Node* cur = pos._node;
			Node* nextNode = cur->_next;
			Node* prevNode = cur->_prev;

			nextNode->_prev = prevNode;
			prevNode->_next = nextNode;

			delete cur;
			_size--;

			return iterator(nextNode);
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = earse(it);				 
			}
			_size = 0;
		}

		void insert(iterator pos, const T& x)
		{
			Node* newnode = new Node(x);

			Node* cur = pos._node;
			Node* prevNode = cur->_prev;

			prevNode->_next = newnode;
			newnode->_prev = prevNode;
			cur->_prev = newnode;
			newnode->_next = cur;

			_size++;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

	private:
		Node* _head;
		size_t _size;
	};
}
